package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.SmsUtils;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 十一
 * @Description
 * @create 2020-11-10-19:48
 */
@RestController
@RequestMapping("order")
@Api(tags = "移动端-预约管理")
public class OrderController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Reference
    private OrderService orderService;

    //发送预约验证码
    @GetMapping("sendCode/{telephone}")
    @ApiOperation(value = "发送手机验证码",notes = "移动端验证码发送")
    public Result findAllSetMeal(@PathVariable("telephone") String telephone){
        //生成手机验证码
        String code = ValidateCodeUtils.generateValidateCode(4);
        //添加进Redis缓存中
        RedisUtil.set(RedisMessageConstant.SENDTYPE_ORDER+telephone,code,90, TimeUnit.SECONDS);
        //将手机号和验证码天剑进消息队列】
        Map<String,String> map = new HashMap();
        map.put("telephone",telephone);
        map.put("code",code);
        rabbitTemplate.convertAndSend("simple-queue",map);
        return new Result(code);
    }

    //查询所有的预约套餐
    @PostMapping("add")
    @ApiOperation(value = "预约",notes = "提交用户预约数据")
    public Result add(@RequestBody Map map){
       return new Result(orderService.add(map));
    }

    //查询订单详细信息
    @GetMapping("findOrderInfoByOrderId/{id}")
    @ApiOperation(value = "查询订单详细信息",notes = "根据订单id查询订单详情")
    public Result findOrderInfoByOrderId(@PathVariable("id") int id){
        return new Result(orderService.findOrderInfoByOrderId(id));
    }

}
